package main

import (
	"bufio"
	"fmt"
	"golang.org/x/net/html/charset"
	"golang.org/x/text/encoding"
	"golang.org/x/text/transform"
	"io"
	"io/ioutil"
	"net/http"
	"regexp"
)

func main() {
	resp, err := http.Get("http://www.dedecms.com/")
	if err != nil {
		panic(err)
	}

	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		fmt.Println("Error:status code", resp.StatusCode)
		return
	}

	// 转编码
	//utf8Reader := transform.NewReader(resp.Body, simplifiedchinese.GBK.NewDecoder())
	e := determineEncoding(resp.Body)
	utf8Reader := transform.NewReader(resp.Body, e.NewDecoder())
	all, err := ioutil.ReadAll(utf8Reader)
	if err != nil {
		panic(err)
	}
	fmt.Printf("content:%s", all)
	printList(all)
}

func printList(contents []byte) {
	// <a target="_blank" href="http://www.7btjia.com/thread-index-fid-1-tid-53983.htm" class="subject_link thread-new">[BT下载][黑金帝国][HD-MP4/1.3G][中文字幕][720P][正义儿子对决走私罪父]</a>
	text := `<a target="_blank" href="(http://www.7btjia.com/thread-index-fid-1-tid-[0-9]+.htm)" [^>]*>[^<]+</a>`
	re := regexp.MustCompile(text)
	match := re.FindAllSubmatch(contents, -1)
	for _, m := range match {
		fmt.Printf("URL = %s ", m[1])
		fmt.Println()
	}
	fmt.Printf("len :", len(match))
}

func determineEncoding(r io.Reader) encoding.Encoding {
	bytes, err := bufio.NewReader(r).Peek(1024)
	if err != nil {
		panic(err)
	}

	e, _, _ := charset.DetermineEncoding(bytes, "")
	return e
}
